package com.csun.cmny.provider.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author liqian
 * @date   2018年3月30日
 * @time   下午3:42:23
 * Description : 菜谱日期工具类
 */
public class MenuTimeUtil {

    private static final int FIRST_DAY = Calendar.MONDAY;

    public static List<String> getDays(String day) {
    	List<String> list = new ArrayList<String>();
    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = dateFormat.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        String week = String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));//第几周
        if (week.equals("53")) {
            week = "1";
        }
        String year = String.valueOf(calendar.get(Calendar.YEAR));//获取年
        if (week.equals("1")) {
            if ((calendar.get(Calendar.MONTH) + 1) == 12) {
                year = String.valueOf((Integer.valueOf(year) + 1));
            }
        }
        list.add(week);
        list.add(year);
        /*for (int i = 0; i < 7; i++) {
        	String str = dateFormat.format(calendar.getTime());
        	list.add(str);
            calendar.add(Calendar.DATE, 1);

        }*/
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }
        for (int i = 0; i < 7; i++) {
            String dayTemp = dateFormat.format(calendar.getTime());
            list.add(dayTemp);
            calendar.add(Calendar.DATE, 1);
        }
        return list;
    }

    //星期天的处理
    public static void setToFirstDay(Calendar calendar) {
        while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
            calendar.add(Calendar.DATE, -1);
        }
    }
}
